package com.xkdx.guangguang.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.comment.BrandCommentFragment;
import com.xkdx.guangguang.fragment.comment.BrandMoreCommentFragment;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.BrandCommentary;
import com.xkdx.guangguang.util.SharePrefenceUtil;
import com.xkdx.guangguang.wiget.TextViewEllipsize;

// 品牌评论适配
public class BrandCommentAdapter extends BaseAdapter {

	String url;
	BaseFragment baseFragment;
	List<BrandCommentary> commlist;
	Context context;
	int num = 0;
	ImageLoader imageLoader;
	String brandID, brandName;
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.shop_empty)
			.showImageForEmptyUri(R.drawable.shop_empty)
			.showImageOnFail(R.drawable.shop_empty).resetViewBeforeLoading()
			.cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
			.bitmapConfig(Bitmap.Config.RGB_565).build();

	public BrandCommentAdapter(BaseFragment baseFragment,
			List<BrandCommentary> list, ImageLoader imageLoader,
			String brandID, String brandName) {
		super();
		this.commlist = list;
		this.baseFragment = baseFragment;
		context = baseFragment.getActivity();
		this.imageLoader = imageLoader;
		this.brandID = brandID;
		this.brandName = brandName;
	}

	@Override
	public int getCount() {

		if (commlist != null && commlist.size() > 0) {
			num = commlist.size();
		}
		return 1 + num + 1;
	}

	@Override
	public Object getItem(int position) {
		if (position == 0) {
			return null;
		} else if (position <= num) {
			int newposition = position - 1;
			return commlist.get(newposition);
		} else {
			return null;
		}

	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (position == 0) {
			View view = null;
			view = LayoutInflater.from(context).inflate(
					R.layout.brandcommentary_header, null);
			TextView tv_brand_commentarynum = (TextView) view
					.findViewById(R.id.tv_brand_commentarynum);
			ImageView tv_brand_commentarywant = (ImageView) view
					.findViewById(R.id.tv_brand_commentarywant);
			if (commlist != null && commlist.size() > 0) {
				tv_brand_commentarynum
						.setText(IConstants.Brand_CommentNum + "");

			} else {
				tv_brand_commentarynum.setText("0");
			}
			tv_brand_commentarywant.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					SharePrefenceUtil su_user = new SharePrefenceUtil(
							baseFragment.getActivity(), IConstants.SP_USER);
					String LoginToken = su_user.getUserLoginToken();
					if (LoginToken.equals("")) {
						baseFragment.UserLogin();
					} else {
						FragmentTransaction frCommentaryInfos = baseFragment
								.getFragmentManager().beginTransaction();
						BrandCommentFragment brandCommentFragment = new BrandCommentFragment();
						Bundle b = new Bundle();
						b.putString("BrandID", brandID);
						b.putString("BrandName", brandName);
						brandCommentFragment.setArguments(b);
						frCommentaryInfos.replace(R.id.fragment,
								brandCommentFragment);
						frCommentaryInfos.addToBackStack(null);
						frCommentaryInfos.commit();
					}
				}
			});

			return view;
		} else if (position > 0 && position <= num) {
			int position0 = position - 1;
			View view = LayoutInflater.from(context).inflate(
					R.layout.shop_commentaryitem, null);
			// cViewHolder = new CommentaryViewHolder();
			ImageView userhead = (ImageView) view
					.findViewById(R.id.iv_shopcommitem_userhead);
			TextView username = (TextView) view
					.findViewById(R.id.tv_shopcommitem_username);
			TextView commentaryDate = (TextView) view
					.findViewById(R.id.tv_shopcommitem_date);
			final TextViewEllipsize commentaryinfos = (TextViewEllipsize) view
					.findViewById(R.id.tv_shopcomitem_infos);
			final ImageView morecommentary = (ImageView) view
					.findViewById(R.id.btn_spcommitem_loadmore);

			username.setText(commlist.get(position0).getNickName() + ":");
			commentaryDate.setText(commlist.get(position0).getCreateTime());
			commentaryinfos
					.setText(commlist.get(position0).getCommentContent());

			commentaryinfos.setEllipsize(TruncateAt.END);
			commentaryinfos.setMaxLines(3);
			String picUrl = commlist.get(position0).getUserAvatars();

			imageLoader.displayImage(picUrl, userhead, options);

			return view;
		} else {
			LinearLayout ll_loadmore = new LinearLayout(context);
			ll_loadmore.setOrientation(LinearLayout.HORIZONTAL);
			TextView tv_commentaryloadmore = new TextView(context);
			tv_commentaryloadmore.setText("查看更多逛逛用户评论 >");
			tv_commentaryloadmore.setBackgroundColor(context.getResources()
					.getColor(R.color.gray_white));
			tv_commentaryloadmore.setTextSize(15.0f);
			tv_commentaryloadmore.setPadding(0, 15, 0, 15);
			tv_commentaryloadmore.setGravity(Gravity.CENTER);

			ll_loadmore.addView(tv_commentaryloadmore,
					new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.MATCH_PARENT,
							LinearLayout.LayoutParams.WRAP_CONTENT));
			ll_loadmore.setGravity(Gravity.CENTER);
			ll_loadmore.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					BrandMoreCommentFragment brandMoreCommentFragment = new BrandMoreCommentFragment();
					Bundle b = new Bundle();
					b.putString("BrandID", brandID);
					b.putString("BrandName", brandName);
					brandMoreCommentFragment.setArguments(b);
					baseFragment.getFragmentManager().beginTransaction()
							.replace(R.id.fragment, brandMoreCommentFragment)
							.addToBackStack(null).commit();
				}
			});
			return ll_loadmore;
		}
	}
}